package inits

import (
	"ecommerce-customer-support-chat/config"
	"ecommerce-customer-support-chat/global"
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
	"log"
)

func InitMysql() {
	var err error
	data := config.Conf.Mysql
	sprintf := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", data.Username, data.Password, data.Host, data.Port, data.Database)

	//dsn := "user:pass@tcp(127.0.0.1:3306)/dbname?charset=utf8mb4&parseTime=True&loc=Local"
	global.DB, err = gorm.Open(mysql.Open(sprintf), &gorm.Config{
		Logger: logger.Default.LogMode(logger.Info),
	})
	if err != nil {
		panic("数据库连接失败")
	} else {
		log.Println("数据库连接成功")
	}
}
